#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

int Index = 0; //窗口索引，判断当前应该打开哪个窗口

void gotoxy( int x, int y){
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void clrscr() {
    system("cls");
}

int ReadMaxScore(){
	return 666;
}

//在（x，y）处绘制SNAKE字体 
void PrintSNAKE(int x,int y){
	//定义"SNAKE"每个字的8x7点阵数据
	const char* S[] = {
    " ###### ",
    "#      #",
    "#       ",
    " ###### ",
    "       #",
    "#      #",
    " ###### "
	};

	const char* N[] = {
    "##     #",
    "# #    #",
    "#  #   #",
    "#   #  #",
    "#    # #",
    "#     ##",
    "#      #"
	};

	const char* A[] = {
    "   ##   ",
    "  #  #  ",
    "  #  #  ",
    " ###### ",
    " #    # ",
    "#      #",
    "#      #"
	};

	const char* K[] = {
    "#      #",
    "#    #  ",
    "#  #    ",
    "# #     ",
    "#  #    ",
    "#    #  ",
    "#      #"
	};

	const char* E[] = {
    "########",
    "#       ",
    "#       ",
    "########",
    "#       ",
    "#       ",
    "########"
	};
	// 在（x，y）处输出"贪吃蛇"艺术字 
	int i;
    for( i=0;i<7;i++) {
    	gotoxy (x,y+i);
        printf("%s  %s  %s  %s  %s", S[i], N[i], A[i],K[i],E[i]);
    }
}

void CreateMenuWindow (){
		//屏幕初始化
		//清屏
		clrscr();
		//获取屏幕缓冲区信息
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
   		GetConsoleScreenBufferInfo(hConsole, &csbi);
   		//获取控制台宽度、高度
  	  	int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  	  	int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
   		//绘制边框，大小为控制台长、宽的4/5，偏移量为1/10
		int padding_width = consoleWidth/10;
		int padding_height = consoleHeight/10;
		//绘制窗口边界
		int i;
		//up
		gotoxy(padding_width,padding_height);
		for (i=padding_width;i<consoleWidth-padding_width;i++){
			putchar('*');
		}
		//down 
		gotoxy(padding_width,consoleHeight-padding_height);
		for (i=padding_width;i<consoleWidth-padding_width;i++){
			putchar('*');
		}
		//left
		for (i=padding_height;i<consoleHeight-padding_height;i++){
			gotoxy(padding_width,i);
			putchar('*');
		}
		//right
		for (i=padding_height;i<consoleHeight-padding_height;i++){
			gotoxy(consoleWidth-padding_width,i);
			putchar('*');
		}
		//绘制左上角信息
		//最高分 
		gotoxy(0,0);
		printf ("历史最高分：%d",ReadMaxScore() );
		//移动操作,F5刷新
		gotoxy(0,1);
		printf ("WASD移动\tF5刷新\n");
		//绘制SNAKE艺术字
		int padding_SNAKE_x = consoleWidth/2-24;
		int padding_SNAKE_y = consoleHeight/5;
		PrintSNAKE(padding_SNAKE_x,padding_SNAKE_y);
		//绘制菜单栏选项
		int padding_oppration_x = consoleWidth/2-3;
		int padding_oppration_y = consoleHeight/2;
		const char* opp[]={
			"开始游戏(\"1\")",
			"游戏说明(\"2\")",
			"退出游戏(\"Esc\")"
		};
		for (i=0;i<sizeof(opp)/sizeof(opp[0]);i++){
			gotoxy(padding_oppration_x,padding_oppration_y+i*consoleHeight/10);
			printf ("%s",opp[i]);
		}
		//接收菜单栏操作
		int tmp;
		while ( 1 ){
			tmp = getch();
			//Esc，退出游戏
			if ( tmp == 0x1b ){
				Index = -1;
				break;
			}
			//F5,刷新窗口
			if ( tmp == 0x0 ){
				tmp=getch();
				if ( tmp == 0x3f ){
					CreateMenuWindow();
					break;
				}
			}
			//1，2，开始游戏，游戏说明
			if ( tmp == '1' ){
                Index = 1;
                break;
            }
            if ( tmp == '2' ){
                Index = 2;
                break;
            }
		}
			return;
	}


int main() {
    CreateMenuWindow();
    return 0;
}